package com.pj.wisdomlogistics.ui.activity;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.pj.wisdomlogistics.MyApplication;
import com.pj.wisdomlogistics.R;
import com.pj.wisdomlogistics.cons.Cons;
import com.pj.wisdomlogistics.entity.MyApplySimpleInfo;
import com.pj.wisdomlogistics.mvp.contract.MyApplyContract;
import com.pj.wisdomlogistics.mvp.frame.MvpActivity;
import com.pj.wisdomlogistics.mvp.frame.base.BaseView;
import com.pj.wisdomlogistics.mvp.presenter.MyApplyPresenter;
import com.pj.wisdomlogistics.other.EventCenter;
import com.pj.wisdomlogistics.ui.adapter.ListDropDownAdapter;
import com.pj.wisdomlogistics.ui.adapter.MyApplySimpleAdapter;
import com.pj.wisdomlogistics.ui.view.recyclerview.LoadMoreRecyclerView;
import com.pj.wisdomlogistics.utils.ActivityManagerUtil;
import com.pj.wisdomlogistics.utils.CustomToast;
import com.yyydjk.library.DropDownMenu;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MyApplyActivity extends MvpActivity<MyApplyPresenter> implements MyApplyContract.View, MyApplySimpleAdapter.onItemClick, SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.dropdownmenu)
    DropDownMenu mDropDownMenu;

    private List<String> mContentList; //类型
    private List<String> mTimeList;    //时间
    private List<String> mStateTakeoutList; //外卖
    private List<String> mStateRepairList;  //维修
    private List<String> mStateOtherList;   //会议，用车,食堂
    private List<String> mCurrentStateList; //当前状态

    private int currentContent = 1;
    private int currentTime = 0;
    private int currentState = -1;

    private MyApplySimpleAdapter mAdapter;

    private List<View> popupViews = new ArrayList<>();
    private String headers[] = {"外卖点餐", "全部", "全部"};

    private ListDropDownAdapter mContentAdapter;
    private ListDropDownAdapter mTimeAdapter;
    private ListDropDownAdapter mStateAdapter;

    private LoadMoreRecyclerView mRvApply;
    private SwipeRefreshLayout mRefreshLayout;

    private int pageIndex = 1;
    private int pageCount = 10;

    private int totalCount;
    private int totalPage;
    private boolean isFirst = true;

    private LinearLayout mLayout;
    private LinearLayout mLlImage;//无数据情况

    @Override
    public void initView() {
        ultimateBar.setColorBar(ContextCompat.getColor(this, R.color.text_blue));

        mLayout = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.layout_refresh_rv, null);
        mRvApply = (LoadMoreRecyclerView) mLayout.findViewById(R.id.recyclerview);
        mRefreshLayout = (SwipeRefreshLayout) mLayout.findViewById(R.id.refresh);
        mLlImage = (LinearLayout) mLayout.findViewById(R.id.ll_noData);

        mRvApply.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new MyApplySimpleAdapter(this);
        mRvApply.setAdapter(mAdapter);
        mAdapter.setListener(this);

        mRvApply.setLoadMoreListener(new LoadMoreRecyclerView.LoadMoreListener() {
            @Override
            public void onLoadMore() {
                mPresenter.myApply(MyApplication.userID, currentContent, currentTime, currentState, ++pageIndex, pageCount);
                mRvApply.notifyMoreFinish(pageIndex < totalPage);
            }
        });


        mRefreshLayout.setColorSchemeResources(
                R.color.google_orange,
                R.color.google_yellow,
                R.color.google_blue,
                R.color.google_green
        );

        mRefreshLayout.setOnRefreshListener(this);


        mTimeList = new ArrayList(Arrays.asList(getResources().getStringArray(R.array.time)));
        mStateTakeoutList = new ArrayList(Arrays.asList(getResources().getStringArray(R.array.state_takeout)));
        mStateRepairList = new ArrayList(Arrays.asList(getResources().getStringArray(R.array.state_repair)));
        mStateOtherList = new ArrayList(Arrays.asList(getResources().getStringArray(R.array.state_other)));
        mCurrentStateList = mStateTakeoutList;


        ListView contentView = new ListView(this);
        contentView.setDividerHeight(0);
        mContentAdapter = new ListDropDownAdapter(this);
        contentView.setAdapter(mContentAdapter);

        ListView timeView = new ListView(this);
        timeView.setDividerHeight(0);
        mTimeAdapter = new ListDropDownAdapter(this);
        mTimeAdapter.setData(mTimeList);
        timeView.setAdapter(mTimeAdapter);

        ListView stateView = new ListView(this);
        stateView.setDividerHeight(0);
        mStateAdapter = new ListDropDownAdapter(this);
        stateView.setAdapter(mStateAdapter);

        switch (MyApplication.userType) {
//            case Cons.UserType.NORMAL_ACCOUNT:
//                //普通帐号
            case Cons.UserType.MANAGE_ACCOUNT_1:
            case Cons.UserType.MANAGE_ACCOUNT_2:
                //管理帐号
                mContentList = new ArrayList(Arrays.asList(getResources().getStringArray(R.array.other_content)));
                break;
            case Cons.UserType.APPLY_ACCOUNT:
                //申请帐号
                mContentList = new ArrayList(Arrays.asList(getResources().getStringArray(R.array.apply_content)));
                break;
        }

        mContentAdapter.setData(mContentList);
        mStateAdapter.setData(mCurrentStateList);

        popupViews.add(contentView);
        popupViews.add(timeView);
        popupViews.add(stateView);

        contentView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mContentAdapter.setCheckItem(position);
                switch (position) {
                    case 0:
                        //外卖点餐
                        mCurrentStateList = mStateTakeoutList;
                        break;
                    case 1:
                        //维护保修
                        mCurrentStateList = mStateRepairList;
                        break;
                    case 2:
                    case 3:
                    case 4:
                        //会议，用车,食堂
                        mCurrentStateList = mStateOtherList;
                        break;
                }
                switch (position) {
                    case 0:
                        currentContent = 1;
                        break;
                    case 1:
                        currentContent = 4;
                        break;
                    case 2:
                        currentContent = 3;
                        break;
                    case 3:
                        currentContent = 2;
                        break;
                    case 4:
                        currentContent = 5;
                        break;
                }
                mStateAdapter.setData(mCurrentStateList);
                mStateAdapter.setCheckItem(0);
                mDropDownMenu.setTabText(mCurrentStateList.get(0), 4);
                mDropDownMenu.setTabText(position == 0 ? headers[0] : mContentList.get(position), 0);
                mDropDownMenu.closeMenu();

                toRefresh();
            }
        });

        timeView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                currentTime = position;
                mTimeAdapter.setCheckItem(position);
                mDropDownMenu.setTabText(position == 0 ? headers[1] : mTimeList.get(position), 2);
                mDropDownMenu.closeMenu();

                toRefresh();
            }
        });

        stateView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                currentState = position - 1;
                mStateAdapter.setCheckItem(position);
                mDropDownMenu.setTabText(position == 0 ? headers[2] : mCurrentStateList.get(position), 4);
                mDropDownMenu.closeMenu();

                toRefresh();
            }
        });

        mDropDownMenu.setDropDownMenu(Arrays.asList(headers), popupViews, mLayout);

        mPresenter.myApply(MyApplication.userID, currentContent, currentTime, currentState, pageIndex, pageCount);
        showProgressDialog();
    }


    @Override
    public void initData() {

    }

    @Override
    public int getContentView() {
        return R.layout.activity_my_apply;
    }

    @Override
    public boolean isApplyEventBus() {
        return true;
    }

    @Override
    public boolean isApplyButterKnife() {
        return true;
    }

    @Override
    public void onEventCome(EventCenter eventCenter) {
        if(eventCenter.getEventCode() == Cons.Other.MY_APPLY){
            toRefresh();
        }
    }

    @Override
    protected BaseView getBaseView() {
        return this;
    }

    @OnClick({R.id.iv_back})
    public void onViewClicked(View mView) {
        switch (mView.getId()) {
            case R.id.iv_back:
                ActivityManagerUtil.create().finishActivity();
                break;

        }
    }


    @Override
    public void itemClick(int type, int id) {
        Intent mIntent = null;
        switch (type) {
            case Cons.ApplyType.APPLY_GWYC:
                mIntent = new Intent(this, CarDetailActivity.class);
                mIntent.putExtra(Cons.CarInfo.CAR_ID , id);
                break;
            case Cons.ApplyType.APPLY_HYS:
                mIntent = new Intent(this, ApplyDetailActivity.class);
                mIntent.putExtra(Cons.ApplyDetail.MEETING_ID, id);
                break;
            case Cons.ApplyType.APPLY_ST:
                mIntent = new Intent(this , MealDetialActivity.class);
                mIntent.putExtra(Cons.MealInfo.MEAL_ID , id);
                break;
            case Cons.ApplyType.APPLY_WHBX:
                mIntent = new Intent(this , RepairDetailActivity.class);
                mIntent.putExtra(Cons.RepairInfo.REPAIR_ID , id);
                break;
            case Cons.ApplyType.APPLY_WMDC:
                mIntent = new Intent(this, TakeoutDetailActivity.class);
                mIntent.putExtra(Cons.TakeoutMeal.TAKEOUT_ID, id);
                break;
        }
        startActivity(mIntent);
    }

    @Override
    public void onRefresh() {
        toRefresh();
    }

    @Override
    public void showError(String errorMsg) {
        dismissProgressDialog();
        CustomToast.showToast(this ,errorMsg , 1000);
    }

    @Override
    public void showSuccess(MyApplySimpleInfo mInfo) {
        dismissProgressDialog();

        if (mRefreshLayout.isRefreshing()) {
            //下拉刷新
            mAdapter.setList(mInfo.getList());
            mRefreshLayout.setRefreshing(false);
        }

        //无数据情况展示
        if (mInfo.getList() == null || mInfo.getList().size() == 0 && pageIndex == 1) {
            mAdapter.setList(mInfo.getList());
            mLlImage.setVisibility(View.VISIBLE);
            mRvApply.setVisibility(View.GONE);
            return;
        } else {
            mLlImage.setVisibility(View.GONE);
            mRvApply.setVisibility(View.VISIBLE);
        }
        if (isFirst) {
            if( mInfo == null || mInfo.getList().size() == 0){
                mAdapter.removeAll();
                return;
            }

            //计算是否需要footview
            totalCount = mInfo.getCount();
            if (pageCount < totalCount) {
                totalPage = totalCount % pageCount == 0 ? totalCount / pageCount : totalCount / pageCount + 1;
            } else {
                totalPage = 1;
            }
            mRvApply.setAutoLoadMoreEnable(pageIndex < totalPage);
            isFirst = false;

            mAdapter.setList(mInfo.getList());
        } else {
            //上拉加载
            mAdapter.appendList(mInfo.getList());
        }


    }

    //下拉框重新选择后，刷新
    private void toRefresh() {
        pageIndex = 1;
        isFirst = true;
        mPresenter.myApply(MyApplication.userID, currentContent, currentTime, currentState, pageIndex, pageCount);
        mRvApply.scrollToPosition(0);
    }
}
